<?php
class CommonTag extends Tag {
	/**
	 * 标签声明
	 * @var array
	 */
	public $Tag = array(
	//显示所有的导航标签
	'navlist' => array('block' => 1, 'level' => 4),
	//显示所有的推荐和热门文章 两篇
	'topart' => array('block' => 1, 'level' => 4),
	//显示所有的文章10篇
	'showAllArt' => array('block' => 1, 'level' => 4),
	//显示 所有的云标签
	'couldTag' => array('block' => 1, 'level' => 4),
	//热闹文件标签
	'hotArt' => array('block' => 1, 'level' => 4),
	//推荐文章列表标签
	'recom' => array('block' => 1, 'level' => 4),
	//友情连接
	'flink' => array('block' =>1 , 'level'=>4),
	//列表页显示所有的相关的文章
	'artlist'=>array('block' =>1 , 'level'=>4),
	//显示相关的 文章
	'relativityArt'=>array('block' =>1 , 'level'=>4),
	//面包屑导航
	'crumbs'=>array('block' =>1 , 'level'=>4),
	//获得子分类
	'sonCate'=>array('block' =>1 , 'level'=>4),
	
	);

	

	
	/**
	 * 导航标签
	 * @param $attr 属性
	 * @param $content 内容
	 * @param $hd HdView模型引擎对象
	 */
	public function _navlist($attr, $content, &$hd) {
		$rows = isset($attr['rows']) ? $attr['rows'] : 10;
		/**
		 * 获得所有的顶级分类 将他们拿来做导航
		 */
		$str = <<<str
<?php
	\$navList = M('category')->field('cid,cname,ctitle,htmldir,is_archtml')->where('pid=0')->limit($rows)->all();
	foreach(\$navList as \$navs):
	\$navs['url'] = getCateUrl(\$navs['is_archtml'],\$navs['htmldir'],\$navs['cid']);
?>
	{$content}
<?php
	endforeach;
?>
str;
		return $str;
	}

	/**
	 * 生成一个遍历所有最新置顶和推荐文章
	 * @param $attr 属性
	 * @param $content 内容
	 * @param $hd HdView模型引擎对象
	 */
	public function _topart($attr, $content, &$hd) {
		/**
		 * 其中这里的$content表示包在这个标签中的内容
		 */
		$rows = isset($attr['rows']) ? $attr['rows'] : 10;
		$attrs = isset($attr['flag']) ? $attr['flag'] : '置顶,推荐';
		$order = isset($attr['order']) ? $attr['order'] : 'sendtime DESC';
		//将他组合为一个数组再将他组合为一个sql where条件
		$attrsArr = explode(',', $attrs);
		$where = '';
		foreach ($attrsArr as $att) {
			$where .= "attr LIKE '%{$att}%' OR ";
		}
		//组合条件语句 只查询 is_recyclye=0的文章表示没有被回收的文章
		$where = '(' . rtrim($where, 'OR ') . ')';
		$where = "is_recycle=0 AND " . $where;

		$str = <<<str
<?php 
\$result = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->field('htmldir,cname,is_archtml,aid,sendtime,click,title,thumb,digest,author,category_cid')->where("{$where}")->order('{$order}')->limit($rows)->all();
foreach(\$result as \$art):
	\$art['thumb']  = '__ROOT__/'.\$art['thumb'];
	//生成连接地址
	\$art['url'] = getStaticPath(\$art['is_archtml'],\$art['htmldir'],\$art['aid']);
?>
	{$content}
<?php
	endforeach;
?>
str;

		return $str;
	}

	/**
	 * 生成一个标签遍历所有的最近发表的文章10默认为10篇
	 */
	public function _showAllArt($attr, $content, &$hd) {
		$rows = isset($attr['rows']) ? $attr['rows'] : 10;
		$order = isset($attr['order']) ? $attr['order'] : 'sendtime DESC';
		//判断如果有传入cid那么表示为分类页面那么就要将他的所有分类和分类相应的文章显示出来

		$str = <<<str
<?php
		\$result = M('article')->field('htmldir,cname,is_archtml,aid,sendtime,click,title,thumb,digest,author,category_cid')->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where('is_recycle=0')->order('$order')->limit($rows)->all();
		foreach(\$result as \$v):
			//组合图片图片
			\$v['thumb']  = '__ROOT__/'.\$v['thumb'];
			\$v['commnum'] = K('Comment')->findCountComment(\$v['aid']);
			\$category = K('Category')->findCate(\$v['category_cid']);
			\$v['category_name'] = \$category['cname'];
			//生成连接地址
			\$v['url'] = getStaticPath(\$v['is_archtml'],\$v['htmldir'],\$v['aid']);
			
			 
?>	
	{$content}
<?php
	endforeach;
?>	
str;
		return $str;
	}

	/**
	 * 遍历所有的云标签
	 */
	public function _couldTag($attr, $content, &$hd) {
		//显示几个云标签
		$rows = isset($attr['rows']) ? $attr['rows'] : 30;
		//		echo 'aaa';

		$str = <<<str
<?php
		\$result = K('Tag')->findAllTagLimit($rows);
		foreach(\$result as \$tag):
			\$tid = \$tag['tid']; 
			\$midd = M('article_tag')->where("tag_tid=\$tid")->all();
			if(!\$midd){
				continue;
			}
?>
	{$content}
<?php
	endforeach;	
?>
str;
		return $str;
	}

	/**
	 *	生成热门文章标签
	 */
	public function _hotArt($attr, $content, &$hd) {
		//行数
		$rows = isset($attr['rows']) ? $attr['rows'] : 8;
		//根据什么排序
		$sort = isset($attr['sort']) ? $attr['sort'] : 'click DESC';

		$str = <<<str
<?php
		\$result = K('article')->findSpeExp('is_recycle=0','{$sort}',$rows);
		foreach(\$result as \$hotart):
		\$hotart['thumb']  = '__ROOT__/'.\$hotart['thumb'];
		\$hotart['commnum'] = K('Comment')->findCountComment(\$hotart['aid']);
		//生成连接地址
		\$hotart['url'] = getStaticPath(\$hotart['is_archtml'],\$hotart['htmldir'],\$hotart['aid']);
?>
		{$content}
<?php
	endforeach;
?>
str;
		return $str;

	}

	/**
	 * 推荐文章标签
	 */
	public function _recom($attr, $content, &$hd) {
		$rows = isset($attr['rows']) ? $attr['rows'] : 8;
		//默认使用推荐attr为推荐的文章
		$att = isset($attr['flag']) ? $attr['flag'] : '推荐';
		//排序
		$sort = isset($attr['sort']) ? $attr['sort'] : 'click DESC';
		
				$str = <<<str
<?php
		\$result = K('article')->findSpeExp('is_recycle=0 AND attr LIKE "%{$att}%"','{$sort}',{$rows});
		foreach(\$result as \$recom):
		\$recom['thumb']  = '__ROOT__/'.\$recom['thumb'];
		\$recom['commnum'] = K('Comment')->findCountComment(\$recom['aid']);
		//生成连接地址
		\$recom['url'] = getStaticPath(\$recom['is_archtml'],\$recom['htmldir'],\$recom['aid']);
?>
		{$content}
<?php
	endforeach;
?>
str;
		return $str;	
		
	}
	
	
	/**
	 * 友情连接标签
	 */
	
	public function _flink($attr,$content,&$hd){
		//遍历多少个友情连接
		$rows = isset($attr['rows'])?$attr['rows']:10;
		//排序
		$sort = isset($attr['sort'])?$attr['sort']:'sort DESC';
		//默认是否显示logo
		$logo = isset($attr['logo'])?$attr['logo']:1;
		
		$str = <<<str
<?php
		\$result = K('flink')->showAllFlink($logo,$rows,'{$sort}');
		foreach(\$result as \$flink):
		\$flink['url'] = 'http://'.\$flink['url'];
		\$flink['logo']  = '__ROOT__/'.\$flink['logo'];
		
?>
	{$content}
<?php
	endforeach;
?>	
str;
		return $str;
	}
	
	
	
	
	/**
	 * 列表页显示相应的标签分类或标签的文章列表
	 */
	public function _artlist($attr,$content,&$hd){
		//如果不分页那么按这个显示
		$rows = isset($attr['row'])?$attr['rows']:1000;
		//默认以发布时间排序
		$sort = isset($attr['sort'])?$attr['sort']:'sendtime DESC'; 
		//默认每页显示多个页码
		$pageNum = isset($attr['pageNum'])?$attr['pageNum']:10;
		//如果分页的话那么按这个页码来
		$pageRows = isset($attr['rows'])?$attr['rows']:15;
		
		$where = ' is_recycle=0 ';
		
		//如果传入的是一个cid那么表示获得相应分类下面所有分类或者子分类的文章
		if($cid = Q('get.cid',0,'intval')){
			//获得所有的分类 
			$categoryModel = K('Category');
			//获得所有的子分类 
			$sons = $categoryModel->resSonCate($categoryModel->all(),$cid);
			if(!$sons){
				$sons = $cid;
			}else{
				$sons = implode(',',$sons).','.$cid;	
			}
			
			$where .= "AND category_cid IN({$sons})";
		}
		if($tid = Q('get.tid',0,'intval')){
			//获得所有相应标签的文章
			$artId = M('article_tag')->where("tag_tid={$tid}")->getField('article_aid',true);
			$artId = implode(',',$artId);
//			echo $artId;die;
			$where .= "AND aid IN({$artId})";
		}
		
		
		$where = ltrim($where,'AND');
		$str = <<<str
<?php
		//总条数
		\$count = M('article')->where("{$where}")->count();
		\$pageObj = new Page(\$count,$pageRows,$pageNum);
		\$page = \$pageObj->show();
		\$result = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->order("{$sort}")->limit(\$pageObj->limit())->all();
		foreach(\$result as \$art):
			\$aid = \$art['aid'];
			\$art['commentcount'] = M('comment')->where("article_aid=\$aid")->count();
			\$art['description'] = M('article_data')->where("article_aid=\$aid")->getField('description');
			\$art['url'] = getStaticPath(\$art['is_archtml'],\$art['htmldir'],\$art['aid']);
			\$c = K('Category')->findCate(\$art['category_cid']);
			\$art['category_name'] = \$c['cname'];
			\$art['curl'] = getCateUrl(\$art['is_listhtml'],\$art['htmldir'],\$art['cid'].'.html');
			\$art['thumb']  = '__ROOT__/'.\$art['thumb'];
?>
	{$content}
<?php
	endforeach;
?>
str;
		return $str;
		
	}
	
	
	/**
	 * 显示关性的文章标签
	 */
	public function _relativityArt($attr,$content,&$hd){
		$aid = Q('get.aid');
		//如果不分页那么按这个显示
		$rows = isset($attr['rows'])?$attr['rows']:1000;
		$cid = M('article')->where("aid={$aid}")->getField('category_cid');		
		
		$str = <<<str
<?php
		\$result = M()->where("category_cid=$cid")->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->limit($rows)->all();
		foreach(\$result as \$field):
		\$field['url'] = getStaticPath(\$field['is_archtml'],\$field['htmldir'],\$field['aid']);
?>
	{$content}
<?php
	endforeach;
?>
str;
		return $str;
	}
	
	
	
	/**
	 * 得到当前分类下方的所有子分类
	 */
	 public function _sonCate($attr,$content,&$hd){
	 	//获得当前分类的ID
	 	$cid = Q('get.cid',0,'intval');
		$data = K('category')->all();
		$getsons = K('category')->resSonCate($data,$cid);
		
		if($getsons){
			$sons = implode(',',$getsons);
			$str = <<<str
<?php
		\$result = K('category')->where("cid IN($sons)")->all();
		foreach(\$result as \$soncate):
			\$soncate['url'] = getCateUrl(\$field['is_listhtml'],\$field['htmldir'],\$soncate['cid'].'.html');
?>	
{$content}
<?php
	endforeach;
?>
str;

				return $str;
		}
			
	 }
	
	
	
	
	
	
	
	
	
	

}
